package android.longs.study.home.servlet.model;

import static org.junit.Assert.*;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;

import android.longs.study.utils.H3Utils;

public class HomeModelTest2 {



    @Test
    public void queryAllFromHQL() {

	Session session = H3Utils.getCurrentSession();
	session.beginTransaction();
	//HQL
	Query query = session.createQuery("from MobleHomeModel");
	//获取
	List<MobleHomeModel> allMobleHomeModel = query.list();

	for (int i = 0; i < allMobleHomeModel.size(); i++) {
	    System.out.println(allMobleHomeModel.get(i).toString());
	}

	session.getTransaction().commit();

    }

    @Test
    public void queryAllFromSQL() {
	Session session = H3Utils.getCurrentSession();
	session.beginTransaction();
	//SQL
	Query query = session.createQuery("select c from MobleHomeModel as c");
	//获取
	List<MobleHomeModel> allMobleHomeModel = query.list();

	for (int i = 0; i < allMobleHomeModel.size(); i++) {
	    System.out.println(allMobleHomeModel.get(i).toString());
	}

	session.getTransaction().commit();
    }

    @Test
    public void queryAllFromQBC() {
	Session session = H3Utils.getCurrentSession();
	session.beginTransaction();
	//QBC
	Criteria query = session.createCriteria(MobleHomeModel.class);
	//获取
	List<MobleHomeModel> allMobleHomeModel = query.list();

	for (int i = 0; i < allMobleHomeModel.size(); i++) {
	    System.out.println(allMobleHomeModel.get(i).toString());
	}

	session.getTransaction().commit();

    }


    @Test
    public void queryForIdFromHQL() {
	Session session = H3Utils.getCurrentSession();
	session.beginTransaction();
	//创建 hql
	MobleHomeModel model =(MobleHomeModel) session.createQuery("from MobleHomeModel c where c.id = 1").uniqueResult();
	System.out.println(model);
	session.getTransaction().commit();
	session.close();
    }
    @Test
    public void queryForIdFromSQL() {
	Session session = H3Utils.getCurrentSession();
	session.beginTransaction();
	//构建sql 
	String sql = "select * from t_home_list c where c.id=1";
	//通过 sql 来查询对应的数据 
	MobleHomeModel model = 
		(MobleHomeModel) session
		.createSQLQuery(sql)//执行sql
		.addEntity(MobleHomeModel.class)//封装数据的实体类型
		.uniqueResult();//获取一个对象


	System.out.println(model);
	session.getTransaction().commit();
	session.close();
    }
    @Test
    public void queryForIdFromQBC() {
	Session session = H3Utils.getCurrentSession();
	session.beginTransaction();

	//QBC 
	MobleHomeModel model = 
		(MobleHomeModel) session
		.createCriteria(MobleHomeModel.class)
		.add(Restrictions.eq("id", 1))	//添加约束
		.uniqueResult();
	System.out.println(model);
	session.getTransaction().commit();
	session.close();
    }

    @Test
    public void queryDescFromHql() {
	Session session = H3Utils.getCurrentSession();
	session.beginTransaction();


	String hql = "from MobleHomeModel c order by c.id desc";
	// 1 hql , 格式：  order by 属性名  [asc]|desc
	List<MobleHomeModel> allCustomer = 
		session.createQuery(hql).list();
	for (int i = 0; i < allCustomer.size(); i++) {
	    MobleHomeModel mobleHomeModel = allCustomer.get(i);
	    System.out.println(mobleHomeModel.id);
	}

	session.getTransaction().commit();

    }
    @Test
    public void queryDescFromSql() {
	Session session = H3Utils.getCurrentSession();
	session.beginTransaction();

	//构建sql 语句 
	String sql = "select * from t_home_list order by id desc";
	// 2 sql 
	List<MobleHomeModel> allCustomer = 
		session.createSQLQuery(sql)
		.addEntity(MobleHomeModel.class)
		.list();


	for (int i = 0; i < allCustomer.size(); i++) {
	    MobleHomeModel mobleHomeModel = allCustomer.get(i);
	    System.out.println(mobleHomeModel.id);
	}

	session.getTransaction().commit();

    }
    @Test
    public void queryDescFromQBC() {
	Session session = H3Utils.getCurrentSession();
	session.beginTransaction();
	// 3 qbc 注意Order
	List<MobleHomeModel> allCustomer =
		session.createCriteria(MobleHomeModel.class)
		.addOrder(org.hibernate.criterion.Order.desc("id"))
		.list();
	for (int i = 0; i < allCustomer.size(); i++) {
	    MobleHomeModel mobleHomeModel = allCustomer.get(i);
	    System.out.println(mobleHomeModel.id);
	}
	session.getTransaction().commit();

    }

}
